//create the database object
var db = Titanium.Database.open('mydb');

db.execute('CREATE TABLE IF NOT EXISTS favorites (ID INTEGER PRIMARY KEY, TITLE TEXT, LINK TEXT, DESCRIPTION TEXT)');

function insertFavorite(title, description, link) {
  var sql = "INSERT INTO favorites (title, description, link) VALUES (";
  sql = sql + "'" + title.replace("'", "''") + "', ";
  sql = sql + "'" + description.replace("'", "''") + "', ";
  sql = sql + "'" + link.replace("'", "''") + "')";
  db.execute(sql);
  return db.lastInsertRowId;
}

function deleteFavorite(id) {
  var sql = "DELETE FROM favorites WHERE id = " + id;
  db.execute(sql);
}

function getFavorites() {
  var sql = "SELECT * FROM favorites ORDER BY title ASC";
  var results = [];
  var resultSet = db.execute(sql);
  while (resultSet.isValidRow()) {
    results.push({
      id: resultSet.fieldByName('id'),
      title: resultSet.fieldByName('title'),
      description: resultSet.fieldByName('description'),
      link: resultSet.fieldByName('link')
    });
    //iterates to the next record
    resultSet.next();
  }
  //you must close the resultset
  resultSet.close();
  //finally, return our array of records!
  return results;
}